package cn.com.vs.api;

import java.text.DateFormat;

import javax.ws.rs.core.MultivaluedMap;

import org.apache.commons.lang3.StringUtils;

import cn.com.vs.service.RedisService;

import com.alibaba.fastjson.JSONObject;

public class ApiUtils {
	public static boolean checkToken( String token,String account ){
		if( StringUtils.isBlank(token))
			return false;
		String result = RedisService.getInstance().getValue(token);
		if( result == null )
			return false;
		else if(StringUtils.isBlank(account) ){
			return false;
		}else{
			JSONObject object = JSONObject.parseObject(result);
			if( account.compareToIgnoreCase(object.getString("account")) == 0 ){
				return true;
			}
		}
		return false;
	}

	public static JSONObject checkAndGetUserInfo( String token,String account ){
		if( StringUtils.isBlank(token))
			return null;
		String result = RedisService.getInstance().getValue(token);
		if( result == null )
			return null;
		else if(StringUtils.isBlank(account) ){
			return null;
		}else{
			JSONObject object = JSONObject.parseObject(result);
			if( account.compareToIgnoreCase(object.getString("account")) == 0 ){
				return object;
			}
		}
		return null;
	}

	public static boolean isAdmin( String token,String account ){
		if( StringUtils.isBlank(token))
			return false;
		String result = RedisService.getInstance().getValue(token);
		if( result == null )
			return false;
		else if(StringUtils.isBlank(account) ){
			return false;
		}else{
			JSONObject object = JSONObject.parseObject(result);
			if( account.compareToIgnoreCase(object.getString("account")) == 0 && object.getIntValue("role") == 1 ){
				return true;
			}
		}
		return false;
	}

	public static boolean isWechatUser( String token,String account ){
		if( StringUtils.isBlank(token))
			return false;
		String result = RedisService.getInstance().getValue(token);
		if( result == null )
			return false;
		else if(StringUtils.isBlank(account) ){
			return false;
		}else{
			JSONObject object = JSONObject.parseObject(result);
			if( account.compareToIgnoreCase(object.getString("account")) == 0 && object.getIntValue("role") == 2 ){
				return true;
			}
		}
		return false;
	}

	public static boolean isWechatUser( JSONObject userInfo ){
		if( userInfo.getIntValue("role") == 2 ){
			return true;
		}
		return false;
	}

	public static String validateJSONRequiredField(JSONObject obj,String[] requires)
	{
		for(String field : requires)
		{
			if( !obj.containsKey(field) )
			{
				return String.format("没有找到[%s]属性", field);
			}
			Object val = obj.get(field);
			if( val == null )
				return String.format("[%s]值不能为null", field);
		}
		return null;
	}

	public static String validateJSONRequiredField(MultivaluedMap<String, String> params,String[] requires)
	{
		for(String field : requires)
		{
			String val = params.getFirst(field);
			if( val == null )
			{
				return String.format("没有找到[%s]属性", field);
			}
			if( StringUtils.isBlank(val) )
				return String.format("[%s]值不能为null", field);
		}
		return null;
	}

	public static Integer Time2UnixTimestamp(String timeStr){
		if( timeStr.length() == 10 ){
			try
			{
				long rt = DateFormat.getDateInstance().parse(timeStr).getTime()/1000;
				return (int)rt;
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
		}else{
			try
			{
				long rt = DateFormat.getDateTimeInstance().parse(timeStr).getTime()/1000;
				return (int)rt;
			}catch(Exception e){
				e.printStackTrace();
				return null;
			}
		}
	}
}
